﻿using System;
using System.Collections.Generic;
using System.Linq;
using ParserGenerator.Collections;

namespace ParserGenerator.ScannerConstruction
{
    [Serializable]
    public sealed class ScannerAutomatonItemSet : ReadOnlySet<ScannerAutomatonItem>
    {
        internal ScannerAutomatonItemSet()
            : base(new HashSet<ScannerAutomatonItem>())
        {
        }

        internal void Add(ScannerAutomatonItem item)
        {
            if (item == null)
                throw new ArgumentNullException(nameof(item));
            Set.Add(item);
        }

        internal void UnionWith(ScannerAutomatonItemSet items)
        {
            if (items == null)
                throw new ArgumentNullException(nameof(items));
            Set.UnionWith(items.Set);
        }

        public bool Equals(ScannerAutomatonItemSet other)
        {
            return other != null && Set.SetEquals(other.Set);
        }

        public sealed override bool Equals(object obj)
        {
            return obj is ScannerAutomatonItemSet && Equals((ScannerAutomatonItemSet)obj);
        }

        public sealed override int GetHashCode()
        {
            return GetSetHashCode();
        }

        public sealed override string ToString()
        {
            var numbers = from x in Set orderby x.Number select x.Number;
            return "{" + string.Join(", ", numbers) + "}";
        }
    }
}
